﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;

namespace RSEngine.Helper.Collection
{

    /// <summary>
    /// This class is a special implementation of the System.Collection.List<T> class
    /// It allow direct access to its inner datas.
    /// </summary>
    [Serializable, DebuggerDisplay("Count = {Count}")]
    public class RSList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
    {

        #region Variables

        private const int _defaultCapacity = 4;
        private static T[] _emptyArray;
        public T[] Items;
        private int _size;

        [NonSerialized]
        private object _syncRoot;
        private int _version;

        #endregion

        #region Constructor

        static RSList()
        {
            RSList<T>._emptyArray = new T[0];
        }

        /// <summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.RSList`1"></see> class that is empty and has the default initial capacity.</summary>
        public RSList()
        {
            this.Items = RSList<T>._emptyArray;
        }

        /// <summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.RSList`1"></see> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.</summary>
        /// <param name="collection">The collection whose elements are copied to the new list.</param>
        /// <exception cref="T:System.ArgumentNullException">collection is null.</exception>
        public RSList(IEnumerable<T> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException(ExceptionArgument.collection);
            }
            ICollection<T> is2 = collection as ICollection<T>;
            if (is2 != null)
            {
                int count = is2.Count;
                this.Items = new T[count];
                is2.CopyTo(this.Items, 0);
                this._size = count;
            }
            else
            {
                this._size = 0;
                this.Items = new T[4];
                using (IEnumerator<T> enumerator = collection.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        this.Add(enumerator.Current);
                    }
                }
            }
        }

        /// <summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.RSList`1"></see> class that is empty and has the specified initial capacity.</summary>
        /// <param name="capacity">The number of elements that the new list can initially store.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">capacity is less than 0. </exception>
        public RSList(int capacity)
        {
            if (capacity < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
            }
            this.Items = new T[capacity];
        }

        #endregion

        #region Features

        /// <summary>Adds an object to the end of the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <param name="item">The object to be added to the end of the <see cref="T:System.Collections.Generic.RSList`1"></see>. The value can be null for reference types.</param>
        public void Add(T item)
        {
            if (this._size == this.Items.Length)
            {
                this.EnsureCapacity(this._size + 1);
            }
            this.Items[this._size++] = item;
            this._version++;
        }

        /// <summary>Adds the elements of the specified collection to the end of the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <param name="collection">The collection whose elements should be added to the end of the <see cref="T:System.Collections.Generic.RSList`1"></see>. The collection itself cannot be null, but it can contain elements that are null, if type T is a reference type.</param>
        /// <exception cref="T:System.ArgumentNullException">collection is null.</exception>
        public void AddRange(IEnumerable<T> collection)
        {
            this.InsertRange(this._size, collection);
        }

        /// <summary>Returns a read-only <see cref="T:System.Collections.Generic.IList`1"></see> wrapper for the current collection.</summary>
        /// <returns>A <see cref="T:System.Collections.Generic.ReadOnlyCollection`1"></see> that acts as a read-only wrapper around the current <see cref="T:System.Collections.Generic.RSList`1"></see>.</returns>
        public ReadOnlyCollection<T> AsReadOnly()
        {
            return new ReadOnlyCollection<T>(this);
        }

        /// <summary>Searches the entire sorted <see cref="T:System.Collections.Generic.RSList`1"></see> for an element using the default comparer and returns the zero-based index of the element.</summary>
        /// <returns>The zero-based index of item in the sorted <see cref="T:System.Collections.Generic.RSList`1"></see>, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of <see cref="P:System.Collections.Generic.RSList`1.Count"></see>.</returns>
        /// <param name="item">The object to locate. The value can be null for reference types.</param>
        /// <exception cref="T:System.InvalidOperationException">The default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default"></see> cannot find an implementation of the <see cref="T:System.IComparable`1"></see> generic interface or the <see cref="T:System.IComparable"></see> interface for type T.</exception>
        public int BinarySearch(T item)
        {
            return this.BinarySearch(0, this.Count, item, null);
        }

        /// <summary>Searches the entire sorted <see cref="T:System.Collections.Generic.RSList`1"></see> for an element using the specified comparer and returns the zero-based index of the element.</summary>
        /// <returns>The zero-based index of item in the sorted <see cref="T:System.Collections.Generic.RSList`1"></see>, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of <see cref="P:System.Collections.Generic.RSList`1.Count"></see>.</returns>
        /// <param name="item">The object to locate. The value can be null for reference types.</param>
        /// <param name="comparer">The <see cref="T:System.Collections.Generic.IComparer`1"></see> implementation to use when comparing elements.-or-null to use the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default"></see>.</param>
        /// <exception cref="T:System.InvalidOperationException">comparer is null, and the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default"></see> cannot find an implementation of the <see cref="T:System.IComparable`1"></see> generic interface or the <see cref="T:System.IComparable"></see> interface for type T.</exception>
        public int BinarySearch(T item, IComparer<T> comparer)
        {
            return this.BinarySearch(0, this.Count, item, comparer);
        }

        /// <summary>Searches a range of elements in the sorted <see cref="T:System.Collections.Generic.RSList`1"></see> for an element using the specified comparer and returns the zero-based index of the element.</summary>
        /// <returns>The zero-based index of item in the sorted <see cref="T:System.Collections.Generic.RSList`1"></see>, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of <see cref="P:System.Collections.Generic.RSList`1.Count"></see>.</returns>
        /// <param name="count">The length of the range to search.</param>
        /// <param name="item">The object to locate. The value can be null for reference types.</param>
        /// <param name="index">The zero-based starting index of the range to search.</param>
        /// <param name="comparer">The <see cref="T:System.Collections.Generic.IComparer`1"></see> implementation to use when comparing elements, or null to use the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default"></see>.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than 0.-or-count is less than 0. </exception>
        /// <exception cref="T:System.InvalidOperationException">comparer is null, and the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default"></see> cannot find an implementation of the <see cref="T:System.IComparable`1"></see> generic interface or the <see cref="T:System.IComparable"></see> interface for type T.</exception>
        /// <exception cref="T:System.ArgumentException">index and count do not denote a valid range in the <see cref="T:System.Collections.Generic.RSList`1"></see>.</exception>
        public int BinarySearch(int index, int count, T item, IComparer<T> comparer)
        {
            if ((index < 0) || (count < 0))
            {
                throw new ArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }
            if ((this._size - index) < count)
            {
                throw new ArgumentException(ExceptionResource.Argument_InvalidOffLen);
            }
            return Array.BinarySearch<T>(this.Items, index, count, item, comparer);
        }

        /// <summary>Removes all elements from the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        public void Clear()
        {
            if (this._size > 0)
            {
                Array.Clear(this.Items, 0, this._size);
                this._size = 0;
            }
            this._version++;
        }

        /// <summary>Determines whether an element is in the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>true if item is found in the <see cref="T:System.Collections.Generic.RSList`1"></see>; otherwise, false.</returns>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.RSList`1"></see>. The value can be null for reference types.</param>
        public bool Contains(T item)
        {
            if (item == null)
            {
                for (int j = 0; j < this._size; j++)
                {
                    if (this.Items[j] == null)
                    {
                        return true;
                    }
                }
                return false;
            }
            EqualityComparer<T> comparer = EqualityComparer<T>.Default;
            for (int i = 0; i < this._size; i++)
            {
                if (comparer.Equals(this.Items[i], item))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>Converts the elements in the current <see cref="T:System.Collections.Generic.RSList`1"></see> to another type, and returns a list containing the converted elements.</summary>
        /// <returns>A <see cref="T:System.Collections.Generic.RSList`1"></see> of the target type containing the converted elements from the current <see cref="T:System.Collections.Generic.RSList`1"></see>.</returns>
        /// <param name="converter">A <see cref="T:System.Converter`2"></see> delegate that converts each element from one type to another type.</param>
        /// <exception cref="T:System.ArgumentNullException">converter is null.</exception>
        public RSList<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)
        {
            if (converter == null)
            {
                throw new ArgumentNullException(ExceptionArgument.converter);
            }
            RSList<TOutput> list = new RSList<TOutput>(this._size);
            for (int i = 0; i < this._size; i++)
            {
                list.Items[i] = converter(this.Items[i]);
            }
            list._size = this._size;
            return list;
        }

        /// <summary>Copies the entire <see cref="T:System.Collections.Generic.RSList`1"></see> to a compatible one-dimensional array, starting at the beginning of the target array.</summary>
        /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.RSList`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
        /// <exception cref="T:System.ArgumentException">The number of elements in the source <see cref="T:System.Collections.Generic.RSList`1"></see> is greater than the number of elements that the destination array can contain.</exception>
        /// <exception cref="T:System.ArgumentNullException">array is null.</exception>
        public void CopyTo(T[] array)
        {
            this.CopyTo(array, 0);
        }

        /// <summary>Copies the entire <see cref="T:System.Collections.Generic.RSList`1"></see> to a compatible one-dimensional array, starting at the specified index of the target array.</summary>
        /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.RSList`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
        /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
        /// <exception cref="T:System.ArgumentException">arrayIndex is equal to or greater than the length of array.-or-The number of elements in the source <see cref="T:System.Collections.Generic.RSList`1"></see> is greater than the available space from arrayIndex to the end of the destination array.</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">arrayIndex is less than 0.</exception>
        /// <exception cref="T:System.ArgumentNullException">array is null.</exception>
        public void CopyTo(T[] array, int arrayIndex)
        {
            Array.Copy(this.Items, 0, array, arrayIndex, this._size);
        }

        /// <summary>Copies a range of elements from the <see cref="T:System.Collections.Generic.RSList`1"></see> to a compatible one-dimensional array, starting at the specified index of the target array.</summary>
        /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.RSList`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
        /// <param name="count">The number of elements to copy.</param>
        /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
        /// <param name="index">The zero-based index in the source <see cref="T:System.Collections.Generic.RSList`1"></see> at which copying begins.</param>
        /// <exception cref="T:System.ArgumentNullException">array is null. </exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than 0.-or-arrayIndex is less than 0.-or-count is less than 0. </exception>
        /// <exception cref="T:System.ArgumentException">index is equal to or greater than the <see cref="P:System.Collections.Generic.RSList`1.Count"></see> of the source <see cref="T:System.Collections.Generic.RSList`1"></see>.-or-arrayIndex is equal to or greater than the length of array.-or-The number of elements from index to the end of the source <see cref="T:System.Collections.Generic.RSList`1"></see> is greater than the available space from arrayIndex to the end of the destination array. </exception>
        public void CopyTo(int index, T[] array, int arrayIndex, int count)
        {
            if ((this._size - index) < count)
            {
                throw new ArgumentException(ExceptionResource.Argument_InvalidOffLen);
            }
            Array.Copy(this.Items, index, array, arrayIndex, count);
        }

        private void EnsureCapacity(int min)
        {
            if (this.Items.Length < min)
            {
                int num = (this.Items.Length == 0) ? 4 : (this.Items.Length * 2);
                if (num < min)
                {
                    num = min;
                }
                this.Capacity = num;
            }
        }

        /// <summary>Determines whether the <see cref="T:System.Collections.Generic.RSList`1"></see> contains elements that match the conditions defined by the specified predicate.</summary>
        /// <returns>true if the <see cref="T:System.Collections.Generic.RSList`1"></see> contains one or more elements that match the conditions defined by the specified predicate; otherwise, false.</returns>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the elements to search for.</param>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public bool Exists(Predicate<T> match)
        {
            return (this.FindIndex(match) != -1);
        }

        /// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.</returns>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the element to search for.</param>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public T Find(Predicate<T> match)
        {
            if (match == null)
            {
                throw new ArgumentNullException(ExceptionArgument.match);
            }
            for (int i = 0; i < this._size; i++)
            {
                if (match(this.Items[i]))
                {
                    return this.Items[i];
                }
            }
            return default(T);
        }

        /// <summary>Retrieves the all the elements that match the conditions defined by the specified predicate.</summary>
        /// <returns>A <see cref="T:System.Collections.Generic.RSList`1"></see> containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty <see cref="T:System.Collections.Generic.RSList`1"></see>.</returns>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the elements to search for.</param>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public RSList<T> FindAll(Predicate<T> match)
        {
            if (match == null)
            {
                throw new ArgumentNullException(ExceptionArgument.match);
            }
            RSList<T> list = new RSList<T>();
            for (int i = 0; i < this._size; i++)
            {
                if (match(this.Items[i]))
                {
                    list.Add(this.Items[i]);
                }
            }
            return list;
        }

        /// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.</returns>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the element to search for.</param>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public int FindIndex(Predicate<T> match)
        {
            return this.FindIndex(0, this._size, match);
        }

        /// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that extends from the specified index to the last element.</summary>
        /// <returns>The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.</returns>
        /// <param name="startIndex">The zero-based starting index of the search.</param>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the element to search for.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">startIndex is outside the range of valid indexes for the <see cref="T:System.Collections.Generic.RSList`1"></see>.</exception>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public int FindIndex(int startIndex, Predicate<T> match)
        {
            return this.FindIndex(startIndex, this._size - startIndex, match);
        }

        /// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that starts at the specified index and contains the specified number of elements.</summary>
        /// <returns>The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.</returns>
        /// <param name="count">The number of elements in the section to search.</param>
        /// <param name="startIndex">The zero-based starting index of the search.</param>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the element to search for.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">startIndex is outside the range of valid indexes for the <see cref="T:System.Collections.Generic.RSList`1"></see>.-or-count is less than 0.-or-startIndex and count do not specify a valid section in the <see cref="T:System.Collections.Generic.RSList`1"></see>.</exception>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public int FindIndex(int startIndex, int count, Predicate<T> match)
        {
            if (startIndex > this._size)
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
            }
            if ((count < 0) || (startIndex > (this._size - count)))
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
            }
            if (match == null)
            {
                throw new ArgumentNullException(ExceptionArgument.match);
            }
            int num = startIndex + count;
            for (int i = startIndex; i < num; i++)
            {
                if (match(this.Items[i]))
                {
                    return i;
                }
            }
            return -1;
        }

        /// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>The last element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.</returns>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the element to search for.</param>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public T FindLast(Predicate<T> match)
        {
            if (match == null)
            {
                throw new ArgumentNullException(ExceptionArgument.match);
            }
            for (int i = this._size - 1; i >= 0; i--)
            {
                if (match(this.Items[i]))
                {
                    return this.Items[i];
                }
            }
            return default(T);
        }

        /// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.</returns>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the element to search for.</param>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public int FindLastIndex(Predicate<T> match)
        {
            return this.FindLastIndex(this._size - 1, this._size, match);
        }

        /// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that extends from the first element to the specified index.</summary>
        /// <returns>The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.</returns>
        /// <param name="startIndex">The zero-based starting index of the backward search.</param>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the element to search for.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">startIndex is outside the range of valid indexes for the <see cref="T:System.Collections.Generic.RSList`1"></see>.</exception>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public int FindLastIndex(int startIndex, Predicate<T> match)
        {
            return this.FindLastIndex(startIndex, startIndex + 1, match);
        }

        /// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that contains the specified number of elements and ends at the specified index.</summary>
        /// <returns>The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.</returns>
        /// <param name="count">The number of elements in the section to search.</param>
        /// <param name="startIndex">The zero-based starting index of the backward search.</param>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the element to search for.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">startIndex is outside the range of valid indexes for the <see cref="T:System.Collections.Generic.RSList`1"></see>.-or-count is less than 0.-or-startIndex and count do not specify a valid section in the <see cref="T:System.Collections.Generic.RSList`1"></see>.</exception>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public int FindLastIndex(int startIndex, int count, Predicate<T> match)
        {
            if (match == null)
                throw new ArgumentNullException(ExceptionArgument.match);

            if (this._size == 0)
            {
                if (startIndex != -1)
                    throw new ArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
            }
            else if (startIndex >= this._size)
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
            }
            if ((count < 0) || (((startIndex - count) + 1) < 0))
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
            }
            int num = startIndex - count;
            for (int i = startIndex; i > num; i--)
            {
                if (match(this.Items[i]))
                {
                    return i;
                }
            }
            return -1;
        }

        /// <summary>Performs the specified action on each element of the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <param name="action">The <see cref="T:System.Action`1"></see> delegate to perform on each element of the <see cref="T:System.Collections.Generic.RSList`1"></see>.</param>
        /// <exception cref="T:System.ArgumentNullException">action is null.</exception>
        public void ForEach(Action<T> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(ExceptionArgument.match);
            }
            for (int i = 0; i < this._size; i++)
            {
                action(this.Items[i]);
            }
        }

        /// <summary>Returns an enumerator that iterates through the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>A <see cref="T:System.Collections.Generic.RSList`1.Enumerator"></see> for the <see cref="T:System.Collections.Generic.RSList`1"></see>.</returns>
        //public Enumerator<T> GetEnumerator()
        //{
        //    return new Enumerator<T>((RSList<T>)this);
        //}

        /// <summary>Creates a shallow copy of a range of elements in the source <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>A shallow copy of a range of elements in the source <see cref="T:System.Collections.Generic.RSList`1"></see>.</returns>
        /// <param name="count">The number of elements in the range.</param>
        /// <param name="index">The zero-based <see cref="T:System.Collections.Generic.RSList`1"></see> index at which the range starts.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than 0.-or-count is less than 0.</exception>
        /// <exception cref="T:System.ArgumentException">index and count do not denote a valid range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see>.</exception>
        public RSList<T> GetRange(int index, int count)
        {
            if ((index < 0) || (count < 0))
            {
                throw new ArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }
            if ((this._size - index) < count)
            {
                throw new ArgumentException(ExceptionResource.Argument_InvalidOffLen);
            }
            RSList<T> list = new RSList<T>(count);
            Array.Copy(this.Items, index, list.Items, 0, count);
            list._size = count;
            return list;
        }

        /// <summary>Searches for the specified object and returns the zero-based index of the first occurrence within the entire <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>The zero-based index of the first occurrence of item within the entire <see cref="T:System.Collections.Generic.RSList`1"></see>, if found; otherwise, –1.</returns>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.RSList`1"></see>. The value can be null for reference types.</param>
        public int IndexOf(T item)
        {
            return Array.IndexOf<T>(this.Items, item, 0, this._size);
        }

        /// <summary>Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that extends from the specified index to the last element.</summary>
        /// <returns>The zero-based index of the first occurrence of item within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that extends from index to the last element, if found; otherwise, –1.</returns>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.RSList`1"></see>. The value can be null for reference types.</param>
        /// <param name="index">The zero-based starting index of the search.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is outside the range of valid indexes for the <see cref="T:System.Collections.Generic.RSList`1"></see>.</exception>
        public int IndexOf(T item, int index)
        {
            if (index > this._size)
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
            }
            return Array.IndexOf<T>(this.Items, item, index, this._size - index);
        }

        /// <summary>Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that starts at the specified index and contains the specified number of elements.</summary>
        /// <returns>The zero-based index of the first occurrence of item within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that starts at index and contains count number of elements, if found; otherwise, –1.</returns>
        /// <param name="count">The number of elements in the section to search.</param>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.RSList`1"></see>. The value can be null for reference types.</param>
        /// <param name="index">The zero-based starting index of the search.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is outside the range of valid indexes for the <see cref="T:System.Collections.Generic.RSList`1"></see>.-or-count is less than 0.-or-index and count do not specify a valid section in the <see cref="T:System.Collections.Generic.RSList`1"></see>.</exception>
        public int IndexOf(T item, int index, int count)
        {
            if (index > this._size)
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
            }
            if ((count < 0) || (index > (this._size - count)))
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
            }
            return Array.IndexOf<T>(this.Items, item, index, count);
        }

        /// <summary>Inserts an element into the <see cref="T:System.Collections.Generic.RSList`1"></see> at the specified index.</summary>
        /// <param name="item">The object to insert. The value can be null for reference types.</param>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than 0.-or-index is greater than <see cref="P:System.Collections.Generic.RSList`1.Count"></see>.</exception>
        public void Insert(int index, T item)
        {
            if (index > this._size)
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
            }
            if (this._size == this.Items.Length)
            {
                this.EnsureCapacity(this._size + 1);
            }
            if (index < this._size)
            {
                Array.Copy(this.Items, index, this.Items, index + 1, this._size - index);
            }
            this.Items[index] = item;
            this._size++;
            this._version++;
        }

        /// <summary>Inserts the elements of a collection into the <see cref="T:System.Collections.Generic.RSList`1"></see> at the specified index.</summary>
        /// <param name="collection">The collection whose elements should be inserted into the <see cref="T:System.Collections.Generic.RSList`1"></see>. The collection itself cannot be null, but it can contain elements that are null, if type T is a reference type.</param>
        /// <param name="index">The zero-based index at which the new elements should be inserted.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than 0.-or-index is greater than <see cref="P:System.Collections.Generic.RSList`1.Count"></see>.</exception>
        /// <exception cref="T:System.ArgumentNullException">collection is null.</exception>
        public void InsertRange(int index, IEnumerable<T> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException(ExceptionArgument.collection);
            }
            if (index > this._size)
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
            }
            ICollection<T> is2 = collection as ICollection<T>;
            if (is2 != null)
            {
                int count = is2.Count;
                if (count > 0)
                {
                    this.EnsureCapacity(this._size + count);
                    if (index < this._size)
                    {
                        Array.Copy(this.Items, index, this.Items, index + count, this._size - index);
                    }
                    if (this == is2)
                    {
                        Array.Copy(this.Items, 0, this.Items, index, index);
                        Array.Copy(this.Items, (int)(index + count), this.Items, (int)(index * 2), (int)(this._size - index));
                    }
                    else
                    {
                        T[] array = new T[count];
                        is2.CopyTo(array, 0);
                        array.CopyTo(this.Items, index);
                    }
                    this._size += count;
                }
            }
            else
            {
                using (IEnumerator<T> enumerator = collection.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        this.Insert(index++, enumerator.Current);
                    }
                }
            }
            this._version++;
        }

        private static bool IsCompatibleObject(object value)
        {
            if (!(value is T) && ((value != null) || typeof(T).IsValueType))
            {
                return false;
            }
            return true;
        }

        /// <summary>Searches for the specified object and returns the zero-based index of the last occurrence within the entire <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>The zero-based index of the last occurrence of item within the entire the <see cref="T:System.Collections.Generic.RSList`1"></see>, if found; otherwise, –1.</returns>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.RSList`1"></see>. The value can be null for reference types.</param>
        public int LastIndexOf(T item)
        {
            return this.LastIndexOf(item, this._size - 1, this._size);
        }

        /// <summary>Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that extends from the first element to the specified index.</summary>
        /// <returns>The zero-based index of the last occurrence of item within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that extends from the first element to index, if found; otherwise, –1.</returns>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.RSList`1"></see>. The value can be null for reference types.</param>
        /// <param name="index">The zero-based starting index of the backward search.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is outside the range of valid indexes for the <see cref="T:System.Collections.Generic.RSList`1"></see>. </exception>
        public int LastIndexOf(T item, int index)
        {
            if (index >= this._size)
            {
                throw new ArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
            }
            return this.LastIndexOf(item, index, index + 1);
        }

        /// <summary>Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that contains the specified number of elements and ends at the specified index.</summary>
        /// <returns>The zero-based index of the last occurrence of item within the range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see> that contains count number of elements and ends at index, if found; otherwise, –1.</returns>
        /// <param name="count">The number of elements in the section to search.</param>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.RSList`1"></see>. The value can be null for reference types.</param>
        /// <param name="index">The zero-based starting index of the backward search.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is outside the range of valid indexes for the <see cref="T:System.Collections.Generic.RSList`1"></see>.-or-count is less than 0.-or-index and count do not specify a valid section in the <see cref="T:System.Collections.Generic.RSList`1"></see>. </exception>
        public int LastIndexOf(T item, int index, int count)
        {
            if (this._size == 0)
            {
                return -1;
            }
            if ((index < 0) || (count < 0))
            {
                throw new ArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }
            if ((index >= this._size) || (count > (index + 1)))
            {
                throw new ArgumentOutOfRangeException((index >= this._size) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
            }
            return Array.LastIndexOf<T>(this.Items, item, index, count);
        }

        /// <summary>Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>true if item is successfully removed; otherwise, false.  This method also returns false if item was not found in the <see cref="T:System.Collections.Generic.RSList`1"></see>.</returns>
        /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.RSList`1"></see>. The value can be null for reference types.</param>
        public bool Remove(T item)
        {
            int index = this.IndexOf(item);
            if (index >= 0)
            {
                this.RemoveAt(index);
                return true;
            }
            return false;
        }

        /// <summary>Removes the all the elements that match the conditions defined by the specified predicate.</summary>
        /// <returns>The number of elements removed from the <see cref="T:System.Collections.Generic.RSList`1"></see> .</returns>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions of the elements to remove.</param>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public int RemoveAll(Predicate<T> match)
        {
            if (match == null)
            {
                throw new ArgumentNullException(ExceptionArgument.match);
            }
            int index = 0;
            while ((index < this._size) && !match(this.Items[index]))
            {
                index++;
            }
            if (index >= this._size)
            {
                return 0;
            }
            int num2 = index + 1;
            while (num2 < this._size)
            {
                while ((num2 < this._size) && match(this.Items[num2]))
                {
                    num2++;
                }
                if (num2 < this._size)
                {
                    this.Items[index++] = this.Items[num2++];
                }
            }
            Array.Clear(this.Items, index, this._size - index);
            int num3 = this._size - index;
            this._size = index;
            this._version++;
            return num3;
        }

        /// <summary>Removes the element at the specified index of the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <param name="index">The zero-based index of the element to remove.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than 0.-or-index is equal to or greater than <see cref="P:System.Collections.Generic.RSList`1.Count"></see>.</exception>
        public void RemoveAt(int index)
        {
            if (index >= this._size)
            {
                throw new ArgumentOutOfRangeException();
            }
            this._size--;
            if (index < this._size)
            {
                Array.Copy(this.Items, index + 1, this.Items, index, this._size - index);
            }
            this.Items[this._size] = default(T);
            this._version++;
        }

        /// <summary>Removes a range of elements from the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <param name="count">The number of elements to remove.</param>
        /// <param name="index">The zero-based starting index of the range of elements to remove.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than 0.-or-count is less than 0.</exception>
        /// <exception cref="T:System.ArgumentException">index and count do not denote a valid range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see>.</exception>
        public void RemoveRange(int index, int count)
        {
            if ((index < 0) || (count < 0))
            {
                throw new ArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }
            if ((this._size - index) < count)
            {
                throw new ArgumentException(ExceptionResource.Argument_InvalidOffLen);
            }
            if (count > 0)
            {
                this._size -= count;
                if (index < this._size)
                {
                    Array.Copy(this.Items, index + count, this.Items, index, this._size - index);
                }
                Array.Clear(this.Items, this._size, count);
                this._version++;
            }
        }

        /// <summary>Reverses the order of the elements in the entire <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        public void Reverse()
        {
            this.Reverse(0, this.Count);
        }

        /// <summary>Reverses the order of the elements in the specified range.</summary>
        /// <param name="count">The number of elements in the range to reverse.</param>
        /// <param name="index">The zero-based starting index of the range to reverse.</param>
        /// <exception cref="T:System.ArgumentException">index and count do not denote a valid range of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see>. </exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than 0.-or-count is less than 0. </exception>
        public void Reverse(int index, int count)
        {
            if ((index < 0) || (count < 0))
            {
                throw new ArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }
            if ((this._size - index) < count)
            {
                throw new ArgumentException(ExceptionResource.Argument_InvalidOffLen);
            }
            Array.Reverse(this.Items, index, count);
            this._version++;
        }

        /// <summary>Sorts the elements in the entire <see cref="T:System.Collections.Generic.RSList`1"></see> using the default comparer.</summary>
        /// <exception cref="T:System.InvalidOperationException">The default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default"></see> cannot find an implementation of the <see cref="T:System.IComparable`1"></see> generic interface or the <see cref="T:System.IComparable"></see> interface for type T.</exception>
        public void Sort()
        {
            this.Sort(0, this.Count, null);
        }

        /// <summary>Sorts the elements in the entire <see cref="T:System.Collections.Generic.RSList`1"></see> using the specified comparer.</summary>
        /// <param name="comparer">The <see cref="T:System.Collections.Generic.IComparer`1"></see> implementation to use when comparing elements, or null to use the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default"></see>.</param>
        /// <exception cref="T:System.ArgumentException">The implementation of comparer caused an error during the sort. For example, comparer might not return 0 when comparing an item with itself.</exception>
        /// <exception cref="T:System.InvalidOperationException">comparer is null, and the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default"></see> cannot find implementation of the <see cref="T:System.IComparable`1"></see> generic interface or the <see cref="T:System.IComparable"></see> interface for type T.</exception>
        public void Sort(IComparer<T> comparer)
        {
            this.Sort(0, this.Count, comparer);
        }

        /// <summary>Sorts the elements in the entire <see cref="T:System.Collections.Generic.RSList`1"></see> using the specified <see cref="T:System.Comparison`1"></see>.</summary>
        /// <param name="comparison">The <see cref="T:System.Comparison`1"></see> to use when comparing elements.</param>
        /// <exception cref="T:System.ArgumentException">The implementation of comparison caused an error during the sort. For example, comparison might not return 0 when comparing an item with itself.</exception>
        /// <exception cref="T:System.ArgumentNullException">comparison is null.</exception>
        public void Sort(Comparison<T> comparison)
        {
            if (comparison == null)
            {
                throw new ArgumentNullException(ExceptionArgument.match);
            }
            if (this._size > 0)
            {
                IComparer<T> comparer = new FunctorComparer<T>(comparison);
                Array.Sort<T>(this.Items, 0, this._size, comparer);
            }
        }

        /// <summary>Sorts the elements in a range of elements in <see cref="T:System.Collections.Generic.RSList`1"></see> using the specified comparer.</summary>
        /// <param name="count">The length of the range to sort.</param>
        /// <param name="index">The zero-based starting index of the range to sort.</param>
        /// <param name="comparer">The <see cref="T:System.Collections.Generic.IComparer`1"></see> implementation to use when comparing elements, or null to use the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default"></see>.</param>
        /// <exception cref="T:System.ArgumentException">index and count do not specify a valid range in the <see cref="T:System.Collections.Generic.RSList`1"></see>.-or-The implementation of comparer caused an error during the sort. For example, comparer might not return 0 when comparing an item with itself.</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than 0.-or-count is less than 0.</exception>
        /// <exception cref="T:System.InvalidOperationException">comparer is null, and the default comparer <see cref="P:System.Collections.Generic.Comparer`1.Default"></see> cannot find implementation of the <see cref="T:System.IComparable`1"></see> generic interface or the <see cref="T:System.IComparable"></see> interface for type T.</exception>
        public void Sort(int index, int count, IComparer<T> comparer)
        {
            if ((index < 0) || (count < 0))
            {
                throw new ArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }
            if ((this._size - index) < count)
            {
                throw new ArgumentException(ExceptionResource.Argument_InvalidOffLen);
            }
            Array.Sort<T>(this.Items, index, count, comparer);
            this._version++;
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return new RSList<T>.RSEnumerator<T>((RSList<T>)this);
        }

        void ICollection.CopyTo(Array array, int arrayIndex)
        {
            if ((array != null) && (array.Rank != 1))
            {
                throw new ArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
            }
            try
            {
                Array.Copy(this.Items, 0, array, arrayIndex, this._size);
            }
            catch (ArrayTypeMismatchException)
            {
                throw new ArgumentException(ExceptionResource.Argument_InvalidArrayType);
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return new RSList<T>.RSEnumerator<T>((RSList<T>)this);
        }

        int IList.Add(object item)
        {
            RSList<T>.VerifyValueType(item);
            this.Add((T)item);
            return (this.Count - 1);
        }

        bool IList.Contains(object item)
        {
            return (RSList<T>.IsCompatibleObject(item) && this.Contains((T)item));
        }

        int IList.IndexOf(object item)
        {
            if (RSList<T>.IsCompatibleObject(item))
            {
                return this.IndexOf((T)item);
            }
            return -1;
        }

        void IList.Insert(int index, object item)
        {
            RSList<T>.VerifyValueType(item);
            this.Insert(index, (T)item);
        }

        void IList.Remove(object item)
        {
            if (RSList<T>.IsCompatibleObject(item))
            {
                this.Remove((T)item);
            }
        }

        /// <summary>Copies the elements of the <see cref="T:System.Collections.Generic.RSList`1"></see> to a new array.</summary>
        /// <returns>An array containing copies of the elements of the <see cref="T:System.Collections.Generic.RSList`1"></see>.</returns>
        public T[] ToArray()
        {
            T[] destinationArray = new T[this._size];
            Array.Copy(this.Items, 0, destinationArray, 0, this._size);
            return destinationArray;
        }

        /// <summary>Sets the capacity to the actual number of elements in the <see cref="T:System.Collections.Generic.RSList`1"></see>, if that number is less than a threshold value.</summary>
        public void TrimExcess()
        {
            int num = (int)(this.Items.Length * 0.9);
            if (this._size < num)
            {
                this.Capacity = this._size;
            }
        }

        /// <summary>Determines whether every element in the <see cref="T:System.Collections.Generic.RSList`1"></see> matches the conditions defined by the specified predicate.</summary>
        /// <returns>true if every element in the <see cref="T:System.Collections.Generic.RSList`1"></see> matches the conditions defined by the specified predicate; otherwise, false. If the list has no elements, the return value is true.</returns>
        /// <param name="match">The <see cref="T:System.Predicate`1"></see> delegate that defines the conditions to check against the elements.</param>
        /// <exception cref="T:System.ArgumentNullException">match is null.</exception>
        public bool TrueForAll(Predicate<T> match)
        {
            if (match == null)
            {
                throw new ArgumentNullException(ExceptionArgument.match);
            }
            for (int i = 0; i < this._size; i++)
            {
                if (!match(this.Items[i]))
                {
                    return false;
                }
            }
            return true;
        }

        private static void VerifyValueType(object value)
        {
            if (!RSList<T>.IsCompatibleObject(value))
                throw new ArgumentException("Wrong type" + typeof(T).FullName); 
        }

        #endregion

        #region Properties

        /// <summary>Gets or sets the total number of elements the internal data structure can hold without resizing.</summary>
        /// <returns>The number of elements that the <see cref="T:System.Collections.Generic.RSList`1"></see> can contain before resizing is required.</returns>
        /// <exception cref="T:System.ArgumentOutOfRangeException"><see cref="P:System.Collections.Generic.RSList`1.Capacity"></see> is set to a value that is less than <see cref="P:System.Collections.Generic.RSList`1.Count"></see>. </exception>
        public int Capacity
        {
            get
            {
                return this.Items.Length;
            }
            set
            {
                if (value != this.Items.Length)
                {
                    if (value < this._size)
                    {
                        throw new ArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
                    }
                    if (value > 0)
                    {
                        T[] destinationArray = new T[value];
                        if (this._size > 0)
                        {
                            Array.Copy(this.Items, 0, destinationArray, 0, this._size);
                        }
                        this.Items = destinationArray;
                    }
                    else
                    {
                        this.Items = RSList<T>._emptyArray;
                    }
                }
            }
        }

        /// <summary>Gets the number of elements actually contained in the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        /// <returns>The number of elements actually contained in the <see cref="T:System.Collections.Generic.RSList`1"></see>.</returns>
        public int Count
        {
            get
            {
                return this._size;
            }
        }

        /// <summary>Gets or sets the element at the specified index.</summary>
        /// <returns>The element at the specified index.</returns>
        /// <param name="index">The zero-based index of the element to get or set.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is less than 0.-or-index is equal to or greater than <see cref="P:System.Collections.Generic.RSList`1.Count"></see>. </exception>
        public T this[int index]
        {
            get
            {
                if (index >= this._size)
                {
                    throw new ArgumentOutOfRangeException();
                }
                return this.Items[index];
            }
            set
            {
                if (index >= this._size)
                {
                    throw new ArgumentOutOfRangeException();
                }
                this.Items[index] = value;
                this._version++;
            }
        }

        bool ICollection<T>.IsReadOnly
        {
            get
            {
                return false;
            }
        }

        bool ICollection.IsSynchronized
        {
            get
            {
                return false;
            }
        }

        object ICollection.SyncRoot
        {
            get
            {
                if (this._syncRoot == null)
                {
                    Interlocked.CompareExchange(ref this._syncRoot, new object(), null);
                }
                return this._syncRoot;
            }
        }

        bool IList.IsFixedSize
        {
            get
            {
                return false;
            }
        }

        bool IList.IsReadOnly
        {
            get
            {
                return false;
            }
        }

        object IList.this[int index]
        {
            get
            {
                return this[index];
            }
            set
            {
                RSList<T>.VerifyValueType(value);
                this[index] = (T)value;
            }
        }

        #endregion

        #region inner class : RSEnumerator

        /// <summary>Enumerates the elements of a <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
        [Serializable, StructLayout(LayoutKind.Sequential)]
        public struct RSEnumerator<T2> : IEnumerator<T2>, IDisposable, IEnumerator
        {
            private RSList<T2> list;
            private int index;
            private int version;
            private T2 current;

            internal RSEnumerator(RSList<T2> list)
            {
                this.list = list;
                this.index = 0;
                this.version = list._version;
                this.current = default(T2);
            }

            /// <summary>Releases all resources used by the <see cref="T:System.Collections.Generic.RSList`1.Enumerator"></see>.</summary>
            public void Dispose()
            {
            }

            /// <summary>Advances the enumerator to the next element of the <see cref="T:System.Collections.Generic.RSList`1"></see>.</summary>
            /// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
            /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
            public bool MoveNext()
            {
                RSList<T2> list = this.list;
                if ((this.version == list._version) && (this.index < list._size))
                {
                    this.current = list.Items[this.index];
                    this.index++;
                    return true;
                }
                return this.MoveNextRare();
            }

            private bool MoveNextRare()
            {
                if (this.version != this.list._version)
                    throw new InvalidOperationException("Invalid operation - enum failed version");

                this.index = this.list._size + 1;
                this.current = default(T2);
                return false;
            }

            /// <summary>Gets the element at the current position of the enumerator.</summary>
            /// <returns>The element in the <see cref="T:System.Collections.Generic.RSList`1"></see> at the current position of the enumerator.</returns>
            public T2 Current
            {
                get
                {
                    return this.current;
                }
            }

            object IEnumerator.Current
            {
                get
                {
                    if ((this.index == 0) || (this.index == (this.list._size + 1)))
                        throw new InvalidOperationException("Invalid operation - enum operation can't happen");

                    return this.Current;
                }
            }

            void IEnumerator.Reset()
            {
                if (this.version != this.list._version)
                    throw new InvalidOperationException("Invalid operation - enum failed version");

                this.index = 0;
                this.current = default(T2);
            }
        }

        #endregion

        #region inner class : FunctorComparer

        internal sealed class FunctorComparer<T> : IComparer<T>
        {
            private Comparer<T> c;
            private Comparison<T> comparison;

            public FunctorComparer(Comparison<T> comparison)
            {
                this.c = Comparer<T>.Default;
                this.comparison = comparison;
            }

            public int Compare(T x, T y)
            {
                return this.comparison(x, y);
            }
        }

        #endregion

        #region ExceptionArgument

        internal static class ExceptionArgument
        {
            static public string obj = "";
            static public string dictionary = "";
            static public string dictionaryCreationThreshold = "";
            static public string array = "";
            static public string info = "";
            static public string key = "";
            static public string collection = "collection";
            static public string list = "";
            static public string match = "";
            static public string converter = "";
            static public string queue = "";
            static public string stack = "";
            static public string capacity = "";
            static public string index = "";
            static public string startIndex = "";
            static public string value = "";
            static public string count = "";
            static public string arrayIndex = "";
            static public string name = "";
            static public string mode = "";
        }

        #endregion

        #region ExceptionResource

        internal static class ExceptionResource
        {
            static public string Argument_ImplementIComparable = "";
            static public string Argument_InvalidType = "";
            static public string Argument_InvalidArgumentForComparison = "";
            static public string Argument_InvalidRegistryKeyPermissionCheck = "";
            static public string ArgumentOutOfRange_NeedNonNegNum = "";
            static public string Arg_ArrayPlusOffTooSmall = "";
            static public string Arg_NonZeroLowerBound = "";
            static public string Arg_RankMultiDimNotSupported = "";
            static public string Arg_RegKeyDelHive = "";
            static public string Arg_RegKeyStrLenBug = "";
            static public string Arg_RegSetStrArrNull = "";
            static public string Arg_RegSetMismatchedKind = "";
            static public string Arg_RegSubKeyAbsent = "";
            static public string Arg_RegSubKeyValueAbsent = "";
            static public string Argument_AddingDuplicate = "";
            static public string Serialization_InvalidOnDeser = "";
            static public string Serialization_MissingKeyValuePairs = "";
            static public string Serialization_NullKey = "";
            static public string Argument_InvalidArrayType = "";
            static public string NotSupported_KeyCollectionSet = "";
            static public string NotSupported_ValueCollectionSet = "";
            static public string ArgumentOutOfRange_SmallCapacity = "";
            static public string ArgumentOutOfRange_Index = "";
            static public string Argument_InvalidOffLen = "";
            static public string Argument_ItemNotExist = "";
            static public string ArgumentOutOfRange_Count = "";
            static public string ArgumentOutOfRange_InvalidThreshold = "";
            static public string ArgumentOutOfRange_ListInsert = "";
            static public string NotSupported_ReadOnlyCollection = "";
            static public string InvalidOperation_CannotRemoveFromStackOrQueue = "";
            static public string InvalidOperation_EmptyQueue = "";
            static public string InvalidOperation_EnumOpCantHappen = "";
            static public string InvalidOperation_EnumFailedVersion = "";
            static public string InvalidOperation_EmptyStack = "";
            static public string ArgumentOutOfRange_BiggerThanCollection = "";
            static public string InvalidOperation_EnumNotStarted = "";
            static public string InvalidOperation_EnumEnded = "";
            static public string NotSupported_SortedListNestedWrite = "";
            static public string InvalidOperation_NoValue = "";
            static public string InvalidOperation_RegRemoveSubKey = "";
            static public string Security_RegistryPermission = "";
            static public string UnauthorizedAccess_RegistryNoWrite = "";
            static public string ObjectDisposed_RegKeyClosed = "";
            static public string NotSupported_InComparableType = "";
        }

        #endregion

    }

}
